package pro.seac.twitter.web.view.component;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import pro.seac.twitter.application.dto.MessageData;
import pro.seac.twitter.web.view.Gravatar;

import com.vaadin.server.ExternalResource;
import com.vaadin.shared.ui.label.ContentMode;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class MessageList extends CustomComponent {
	private static final long serialVersionUID = 1L;
	private List<MessageData> messages;
	private VerticalLayout container;
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public MessageList() {
		super();
		initContent();
	}

	protected void initContent() {
		container = new VerticalLayout();
		container.setWidth("100%");
		container.setSpacing(true);
		setCompositionRoot(container);
	}

	private void repaint() {
		container.removeAllComponents();
		if (CollectionUtils.isNotEmpty(messages)) {
			for (MessageData msg : messages) {
				MessageComponent msgComponent;
				container.addComponent(msgComponent = new MessageComponent(msg));
				container.setComponentAlignment(msgComponent, Alignment.TOP_CENTER);
				Label label = null;
				container.addComponent(label = new Label("<hr/>", ContentMode.HTML));
				label.setWidth(100, Unit.PERCENTAGE);
				label.setStyleName(Reindeer.LABEL_H2);
			}
		} else {
			Label label = new Label("Brak wiadomo\u015Bci do wy\u015Bwietlenia");
			label.setStyleName(Reindeer.LABEL_SMALL);
			container.addComponent(label);
		}
	}

	public void refreshMessages(List<MessageData> messages) {
		this.messages = messages;
		repaint();
	}

	public class MessageComponent extends CustomComponent {
		private static final long serialVersionUID = 1L;
		private Label messageContent;
		private MessageData message;

		public MessageComponent(MessageData message) {
			super();
			this.message = message;
			initContent();
		}

		protected void initContent() {
			HorizontalLayout container = new HorizontalLayout();
			container.setSpacing(true);
//			container.setMargin(true);
			container.setWidth("100%");
//			container.setStyleName(Reindeer.LAYOUT_BLUE);

			{// *** avatar
				Embedded senderAvatar = new Embedded(null, new ExternalResource(Gravatar.getAvatarUrl(message.getSender().getEmail(), "48")));
				senderAvatar.setWidth("48px");
				senderAvatar.setHeight("48px");
				container.addComponent(senderAvatar);
			}

			{// *** content
				VerticalLayout content = new VerticalLayout();
				content.setSpacing(true);

				{// *** title
					HorizontalLayout title = new HorizontalLayout();
					title.setSpacing(true);

					Label userName = new Label(message.getSender().getName() + " (@" + message.getSender().getUserName() + ")");
					userName.setStyleName(Reindeer.LABEL_H2);
					title.addComponent(userName);
					title.setComponentAlignment(userName, Alignment.BOTTOM_LEFT);

					Label created = new Label(formatter.format(message.getCreated()));
					created.setStyleName(Reindeer.LABEL_SMALL);
					title.addComponent(created);
					title.setComponentAlignment(created, Alignment.MIDDLE_LEFT);

					content.addComponent(title);
				}

				{// *** message
					messageContent = new Label();
					messageContent.setValue(message.getContent());

					content.addComponent(messageContent);
					content.setExpandRatio(messageContent, 1);
				}

				container.addComponent(content);
				container.setExpandRatio(content, 1);
			}
			setCompositionRoot(container);
		}
	}

}
